package com.vanke.vms.management.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.omg.CORBA_2_3.portable.OutputStream;

//import javax.servlet.http.HttpServletRequest;
//
//import org.apache.struts2.ServletActionContext;

import com.vanke.vms.common.action.BaseAction;
import com.vanke.vms.common.domain.Info;
import com.vanke.vms.common.domain.Pair;
import com.vanke.vms.common.domain.User;
import com.vanke.vms.management.service.AddInfoService;

public class AddInfoAction extends BaseAction{

	/**
	 * Auto Generated
	 */
	private static final long serialVersionUID = 3866306296487673588L;

	private int role;

	private List<Pair> columnList;
	
	private AddInfoService addInfoService;
	
	private Info info;
	
	public String showColumn(){
		User curUser = (User)session.get("user");
		if(curUser == null)
			return LOGIN;
		if(curUser.getGrade() == 2)
			role = 2;
		else if(curUser.getGrade() == 1)
			role = 1;
		else if(curUser.getGrade() == 0)
			role = 0;
		//define the stable Arrays in the JSP page, below add the additional info
		//HttpServletRequest request = ServletActionContext.getRequest();
		columnList = addInfoService.addList();
		//request.setAttribute("comment", columnList);
		return SUCCESS;
	}
	
	public String addInfo(){
		User curUser = (User)session.get("user");
		if(curUser == null)
			return LOGIN;
		if(curUser.getGrade() == 2)
			role = 2;
		else if(curUser.getGrade() == 1)
			role = 1;
		else if(curUser.getGrade() == 0)
			role = 0;
		
		if(isDuplicated(info.getHouseSet(),info.getHouseBuild(),info.getHouseNumber()))
			return "duplicated";
		String result = addInfoService.addInfo(info);
		addInfoService.addAddInfo(result, columnList);
		return "success";
	}
	
	public boolean isDuplicated(String houseSet, String houseBuild, String houseNum){
		return addInfoService.isDublipcated(houseSet, houseBuild, houseNum);
	}
	
////////////////////////////////////////////////////////////////////
	
	
	public int getRole() {
		return role;
	}

	public void setRole(int role) {
		this.role = role;
	}
	
	/**
	 * @return the info
	 */
	public Info getInfo() {
		return info;
	}

	/**
	 * @param info the info to set
	 */
	public void setInfo(Info info) {
		this.info = info;
	}

	/**
	 * @return the serialversionuid
	 */
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	/**
	 * @return the addInfoService
	 */
	public AddInfoService getAddInfoService() {
		return addInfoService;
	}

	/**
	 * @param addInfoService the addInfoService to set
	 */
	public void setAddInfoService(AddInfoService addInfoService) {
		this.addInfoService = addInfoService;
	}

	/**
	 * @return the columnList
	 */
	public List<Pair> getColumnList() {
		return columnList;
	}

	/**
	 * @param columnList the columnList to set
	 */
	public void setColumnList(List<Pair> columnList) {
		this.columnList = columnList;
	}
}
